

import '../utils/Settings.dart';
import '../utils/StoreData.dart';
import '../utils/dio/DioMethod.dart';
import '../utils/dio/DioUtil.dart';

class DictApi{
  static const dictInfoUrl = "${Settings.appServer}/SysDictController/getDictInfo";

  ///请求字典信息
  static dynamic getDictInfo (String dictCode,String params) async {
    return await DioUtil.getInstance().request(dictInfoUrl,method:DioMethod.get,params: {"dictCode":dictCode,"dictParam":params??""});
  }

  ///初始化字典信息
  static dynamic initDict(String dictCodes,String params,List dictCodeReplaceFields)async{
    if(dictCodes.isEmpty){
      return;
    }
    List dictCodeArr = dictCodes.toString().split(",");
    List newDictCodes = [];
    for(var dictCode in dictCodeArr){
      if(StoreData.dictInfo[dictCode]==null){
        newDictCodes.add(dictCode);
      }
    }

    if(newDictCodes.isEmpty){
      return;
    }

    dynamic res =  await getDictInfo(newDictCodes.join(","), params??"");
    if(!res["success"]){
      return;
    }
    res["result"].forEach((key,value){
        StoreData.dictInfo[key] = value;
    });
  }

}